class CandidatsController < ApplicationController
  before_filter :find_candidat, :only => [:show, :edit, :update, :destroy]
  before_filter :est_connecte, :only => [:edit, :update, :destroy]
  before_filter :est_proprietaire, :only => [:edit, :update, :destroy]
  before_filter :disconnecte, :only => [:new]

  def find_candidat
    @candidat = Candidat.find(params[:id])
    @user = @candidat.user
  end

  # GET /candidats
  # GET /candidats.xml
  def index
    @candidats = Candidat.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @candidats }
    end
  end

  # GET /candidats/1
  # GET /candidats/1.xml
  def show
    respond_to do |format|
      format.html # _question.html.erb
      format.xml  { render :xml => @candidat }
    end
  end

  # GET /candidats/new
  # GET /candidats/new.xml
  def new
    @candidat = Candidat.new
    @user = User.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @candidat }
    end
  end

  # GET /candidats/1/edit
  def edit
  end

  # POST /candidats
  # POST /candidats.xml
  def create
    @candidat = Candidat.new(params[:candidat])
    @user = @candidat.build_user(params[:candidat][:user_attributes])
    if params[:candidat][:photo]
      params[:candidat][:photo] = upload_photo
    end
    respond_to do |format|
      if @candidat.save
        flash[:notice] = 'Votre inscription en tant que candidat est réussi.'
        @template.sign_in @user
        format.html { redirect_to @candidat }
        format.xml  { render :xml => @candidat, :status => :created, :location => @candidat }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @candidat.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /candidats/1
  # PUT /candidats/1.xml
  def update
    if params[:candidat][:photo]
      if File.exist?("public/images/candidats/#{@candidat.photo}")
        File.delete("public/images/candidats/#{@candidat.photo}")
      end
      params[:candidat][:photo] = upload_photo
    end
    respond_to do |format|
      if params[:candidat][:photo] == false
        flash[:error] = "Format fichier non conforme."
        format.html { render :action => "edit" }
      elsif @candidat.update_attributes(params[:candidat])
        flash[:notice] = 'Vos informations ont bien été mise à jour.'
        format.html { redirect_to(@candidat) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @candidat.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /candidats/1
  # DELETE /candidats/1.xml
  def destroy
    @candidat.destroy

    respond_to do |format|
      format.html { redirect_to(candidats_url) }
      format.xml  { head :ok }
    end
  end

  def est_connecte
    unless @template.signed_in?
      flash[:notice] = "Veuillez-vous connecter pour accéder à cette page."
      redirect_to connexion_path
    end
  end

  def est_proprietaire
    unless @template.current_user == @user
      flash[:notice] = "Vous n'avez pas le droit d'accéder à cette page."
      begin
        redirect_to :back
      rescue ActionController::RedirectBackError
        redirect_to root_path
      end
    end
  end

  def disconnecte
    if @template.signed_in?
      flash[:notice] = "Veuillez-vous déconnecter pour accéder à cette page."
      begin
        redirect_to :back
      rescue ActionController::RedirectBackError
        redirect_to root_path
      end
    end
  end

  # charge l'image envoyé sur le serveur si celle-ci est de type jpeg ou png
  # sinon retourne false
  def upload_photo
    if params[:candidat][:photo].content_type.eql?("image/jpeg")
      name =  "#{params[:candidat][:nom]}_#{params[:candidat][:prenom]}.jpeg"
    elsif params[:candidat][:photo].content_type.eql?("image/png")
      name =  "#{params[:candidat][:nom]}_#{params[:candidat][:prenom]}.png"
    else
      return false
    end
    directory = "public/images/candidats"
    # create the file path
    path = File.join(directory, name)
    # write the file
    f = File.open(path, "wb") do |f|
      f.write(params[:candidat][:photo].read)
    end
    return name
  end
end
